#!/bin/sh
stty -echo
project_suffix=$1;
backup_dir="/opt/www/wwwroot/backup/";
zip_dir="/opt/www/wwwroot/backup/"
project_dir="/opt/www/wwwroot/";
time_now=$(date "+%Y%m%d%H%M%S")
if [ ! $project_suffix ];then
    echo "you should run this like: ./redeploy.sh project_name";
    exit;
fi
if [ ! -d ${project_dir}${project_suffix} ]; then
    echo ${project_dir}${project_suffix} not exist,please check your zip name;
    exit;
fi
if [ ! -e ${zip_dir}${project_suffix}.zip ];then
    echo ${zip_dir}${project_suffix}.zip not exist;
    exit;
fi
# 解压
mv ${zip_dir}${project_suffix}.zip ${backup_dir}${project_suffix}/${project_suffix}_backup${time_now}.zip
cd ${backup_dir}${project_suffix}
unzip ${backup_dir}${project_suffix}/${project_suffix}_backup${time_now}.zip
if [ ! -e ${backup_dir}${project_suffix}/${project_suffix}/index.html ];then
    echo index.html not exist,please check zip
	 rm -rf ${backup_dir}${project_suffix}/${project_suffix}
	 rm -rf ${backup_dir}${project_suffix}/${project_suffix}_backup${time_now}.zip
    exit;
fi
# 备份项目(因为有之前的压缩包，就不备份了，占空间太严重)
rm -rf ${project_dir}${project_suffix} ${backup_dir}${project_suffix}/${project_suffix}_backup${time_now}
# echo 已将当前项目备份到${backup_dir}${project_suffix}/${project_suffix}_backup${time_now}
# 重命名文件夹&清理压缩包
mv ${backup_dir}${project_suffix}/${project_suffix} ${project_dir}${project_suffix}
echo redeploy success;
